﻿using Caliburn.Micro;
using KouDai.Phone.Engine;
using KouDai.Phone.Helper;
using KouDai.Phone.Models;
using KouDai.Phone.Services;
using KouDai.Phone.Utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace KouDai.Phone.ViewModels
{
    public class CategoryViewModel : KouDaiScreen
    {
        
        #region Fields
        private INavigationService _navigationService;
        private ObservableCollection<SearchCategoryEntity> _categoryItems = new ObservableCollection<SearchCategoryEntity>();
        #endregion

        #region Constructor
        public CategoryViewModel()
        {
            if (Execute.InDesignMode)
            {
                LoadDesignData();
            }
        }

        public CategoryViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
        }
        #endregion

        #region Properties
        public ObservableCollection<SearchCategoryEntity> CategoryItems
        {
            get { return _categoryItems; }
            set
            {
                if (value != _categoryItems)
                {
                    _categoryItems = value;
                    this.NotifyOfPropertyChange(() => this.CategoryItems);
                }
            }
        }
        #endregion

        #region Override
        public override void LoadDesignData()
        {
            List<SearchCategoryEntity> categoryList = new List<SearchCategoryEntity>();

            var aa1 = new SearchCategoryEntity();
            aa1.Id = "1";
            aa1.ItemTagName = "女装";
            aa1.ImageUrl = "http://img.geilicdn.com/93c74b30f76bb5a358f500ec2949d36c_200x200.png";
            categoryList.Add(aa1);

            var aa2 = new SearchCategoryEntity();
            aa2.Id = "1";
            aa2.ItemTagName = "女装";
            aa2.ImageUrl = "http://img.geilicdn.com/93c74b30f76bb5a358f500ec2949d36c_200x200.png";
            categoryList.Add(aa2);

            var aa3 = new SearchCategoryEntity();
            aa3.Id = "1";
            aa3.ItemTagName = "女装";
            aa3.ImageUrl = "http://img.geilicdn.com/93c74b30f76bb5a358f500ec2949d36c_200x200.png";
            categoryList.Add(aa3);

            var aa4 = new SearchCategoryEntity();
            aa4.Id = "1";
            aa4.ItemTagName = "女装";
            aa4.ImageUrl = "http://img.geilicdn.com/93c74b30f76bb5a358f500ec2949d36c_200x200.png";
            categoryList.Add(aa4);

            var aa5 = new SearchCategoryEntity();
            aa5.Id = "1";
            aa5.ItemTagName = "女装";
            aa5.ImageUrl = "http://img.geilicdn.com/93c74b30f76bb5a358f500ec2949d36c_200x200.png";
            categoryList.Add(aa5);

            var aa6 = new SearchCategoryEntity();
            aa6.Id = "1";
            aa6.ItemTagName = "女装";
            aa6.ImageUrl = "http://img.geilicdn.com/93c74b30f76bb5a358f500ec2949d36c_200x200.png";
            categoryList.Add(aa6);

            var aa7 = new SearchCategoryEntity();
            aa7.Id = "1";
            aa7.ItemTagName = "女装";
            aa7.ImageUrl = "http://img.geilicdn.com/93c74b30f76bb5a358f500ec2949d36c_200x200.png";
            categoryList.Add(aa7);

            var aa8 = new SearchCategoryEntity();
            aa8.Id = "1";
            aa8.ItemTagName = "女装";
            aa8.ImageUrl = "http://img.geilicdn.com/93c74b30f76bb5a358f500ec2949d36c_200x200.png";
            categoryList.Add(aa8);

            var aa9 = new SearchCategoryEntity();
            aa9.Id = "1";
            aa9.ItemTagName = "女装";
            aa9.ImageUrl = "http://img.geilicdn.com/93c74b30f76bb5a358f500ec2949d36c_200x200.png";
            categoryList.Add(aa9);

            var aa10 = new SearchCategoryEntity();
            aa10.Id = "1";
            aa10.ItemTagName = "女装";
            aa10.ImageUrl = "http://img.geilicdn.com/93c74b30f76bb5a358f500ec2949d36c_200x200.png";
            categoryList.Add(aa10);

            categoryList.ToObservableCollection<SearchCategoryEntity>(this.CategoryItems);
        }

        protected override void OnInitialize()
        {
            GetSearchCategory();
        }
        #endregion

        #region Method
        public async void GetSearchCategory()
        {
            BackData backData = await ServiceManager.GetSearchCategory("");
            if (backData.IsSuccess)
            {
                List<SearchCategoryEntity> categoryItems = backData.ResposeData as List<SearchCategoryEntity>;
                categoryItems.ToObservableCollection<SearchCategoryEntity>(this.CategoryItems);
            }
            else
            {
                Debug.WriteLine("msg:" + backData.Msg);
            }
        }

        public void SecondLevelCategory(SearchCategoryEntity selectedItem)
        {
            if (selectedItem == null)
            {
                throw new ArgumentNullException("selectedItem");
            }
            _navigationService.UriFor<SecondCategoryPageViewModel>().WithParam<string>(x => x.CategoryId, selectedItem.Id)
                .WithParam<string>(x => x.Title, selectedItem.ItemTagName).Navigate();
            //_navigationService.UriFor<NewSecondCategoryPageViewModel>().WithParam<string>(x => x.CategoryId, selectedItem.Id)
            //   .WithParam<string>(x => x.Title, selectedItem.ItemTagName).Navigate();
        }
        #endregion

        #region ViewMethod
        
        #endregion
    }
}
